
var cloudwalkobj;
var width=1;
var height=1;
var CW_StrSeverIp = "120.25.161.56:7000";
var CW_appID = "user";
var CW_appSecret = "12345";
var CW_License = "";
var imgdata = "";


function createPlugin(targetE) {
	cloudwalkobj = document.createElement("object");
	cloudwalkobj.width = width;
	cloudwalkobj.height = height;
	cloudwalkobj.id = "CloudWalkSDKPlugin";
	
	var bDet=BrowserDetect.browser;
	if("Explorer"==bDet)
	{
		cloudwalkobj.classid = "CLSID:B1597418-A51E-4140-8698-EE865439755C";	//IE
	}else if("Firefox"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Firefox
	}else if("Chrome"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Chrome
	}else{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";
	}

	targetE.appendChild(cloudwalkobj);
	
	if(!cloudwalkobj.valid){
		alert("未安装控件");
		return false;
	}
	
	//获取版本号
	var version = cloudwalkobj.cwGetVersion();
	//alert(version);
	
	var config = cloudwalkobj.cwGetConfig();
	PraseConfig(config);
	
	var nRet = cloudwalkobj.cwInit(CW_License);
	if(nRet != 0)
	{
		var str = "初始化失败，错误码：" + nRet;
		alert(str);
	}
	return true;
}


function click_browseFaceImgFilePath()
{
	var imgPath=cloudwalkobj.cwSelectFile();
	if(imgPath.length ==0)
	{
		return;
	}
	
	document.getElementById("fileText").value = "";
	document.getElementById("imghead").src = '../images/tianjiazhaopian.png';
	
	imgdata = cloudwalkobj.cwGetBase64ByLocalFile(imgPath);
	if(imgdata == "" || imgdata == null)
	{
		alert("加载图片失败");
		return;
	}

	document.getElementById("fileText").value = imgPath;
	var ImgObj = document.getElementById("imghead");
	
	showImg(ImgObj, imgPath, imgdata);
}

function click_getFaceImg()
{
	if(imgdata.length ==0)
	{
		alert("请先选择图片！");
		return;
	}
	
	var jsonStr = cloudwalkobj.cwDetectFace(imgdata);
	var jsonObj = $.parseJSON(jsonStr);
	if(jsonObj.result != 0){
		var str= "人脸检测失败，错误码：" + jsonObj.result;
		alert(str);
		return;
	}
	
	var Tempimg = jsonObj.image;
	var imgObj = document.getElementById("faceImg");
	
	if(isIE6_8())
	{
		var saveDir = "C:/cloudwalk/"; 					//图片保存的所在目录
		var filename = new Date().getTime()+".jpg";		//图片名称，如果文件名已存在则直接覆盖
		//var filename = "bestFace.jpg";				//文件名固定时，使用img.src = path;不能及时更新，还是显示上一张
		var filepath = saveDir + filename;
		var nret = cloudwalkobj.cwBase64ToFile(Tempimg, saveDir, filename);		//Base64保存为本地图片
		imgObj.src = 'file:///'+filepath;			//在Firefox下面必须添加file:///前缀才可以正确显示图片，IE有无皆可
	}
	else
	{
		imgObj.src = "data:image/jpeg;base64,"+ Tempimg;
	}
}

function openCramera()
{
	var nRet = cloudwalkobj.cwStartCamera(0,0);
	
	if(nRet != 0)
	{
		var msg = "打开摄像头失败错误码:"  + nRet;
		alert(msg);
	}
	
	return nRet
}

function closeCramera()
{
	cloudwalkobj.cwStopCamera();
}

function takePicture()
{
	var Tempimg = cloudwalkobj.cwTakePictures();
	if(Tempimg == null || Tempimg.length == 0)
	{
		alert("拍照失败！");
		return;
	}
	if(ImgObjName == "imghead")
	{
		imgdata = Tempimg;
		document.getElementById("fileText").value = "";
	}
	var faceimg = document.getElementById(ImgObjName);
	if(isIE6_8())
	{
		var saveDir = "C:/cloudwalk/"; 					//图片保存的所在目录
		var filename = new Date().getTime()+".jpg";		//图片名称，如果文件名已存在则直接覆盖
		//var filename = "bestFace.jpg";				//文件名固定时，使用img.src = path;不能及时更新，还是显示上一张
		var filepath = saveDir + filename;
		var nret = cloudwalkobj.cwBase64ToFile(Tempimg, saveDir, filename);		//Base64保存为本地图片
		faceimg.src = 'file:///'+filepath;			//在Firefox下面必须添加file:///前缀才可以正确显示图片，IE有无皆可
	}
	else
	{
		faceimg.src = "data:image/jpeg;base64,"+ Tempimg;
	}
	
	$("#question").css("display","block");
	showMessageInfo("");
}

function PraseConfig(strJson)
{
	var jsonObj = $.parseJSON(strJson);
	if(jsonObj != "null")
	{
	 CW_StrSeverIp = jsonObj["ip"];
	 CW_appID =  jsonObj["appID"];
	 CW_appSecret =  jsonObj["appSecret"];
	 CW_License = jsonObj["License"];
	}
}

function registerCallBackEach()
{
}

